package com.huawei.hwid.update.d;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.f.d;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

public abstract class a implements com.huawei.hwid.activity.a {
    public WeakReference<Activity> a;
    public com.huawei.hwid.activity.a b;
    protected j c = null;
    protected b d = null;
    protected boolean e = false;
    protected int f = -1;
    protected String g = null;
    protected String h = null;
    protected int i = 0;

    abstract void a(Class<? extends b> cls);

    abstract void c();

    public static String a(int i) {
        switch (i) {
            case 0:
                return i.class.getName();
            case 5:
                return f.class.getName();
            case 6:
                return k.class.getName();
            default:
                return "";
        }
    }

    public void a(b bVar) {
    }

    public void b(b bVar) {
    }

    protected Activity d() {
        if (this.a == null) {
            return null;
        }
        return (Activity) this.a.get();
    }

    public void a(Activity activity) {
        e.b("AbsUpdateWizard", getClass().getSimpleName() + " onBridgeActivityCreate", true);
        this.a = new WeakReference(activity);
        if (this.c == null) {
            Intent intent = activity.getIntent();
            if (intent != null) {
                this.c = (j) intent.getSerializableExtra(BridgeActivity.EXTRA_DELEGATE_UPDATE_INFO);
                if (this.c == null) {
                    return;
                }
            }
            return;
        }
        this.g = this.c.a();
        this.h = this.c.c();
        this.i = this.c.b();
        this.b = null;
        this.e = false;
        this.f = -1;
    }

    public void a() {
        this.a = null;
        e();
        if (this.e && this.b != null) {
            this.b.a();
        }
    }

    public void b() {
        if (this.e && this.b != null) {
            this.b.b();
        } else if (this.d != null) {
            Class cls = this.d.getClass();
            this.d.c();
            this.d = null;
            a(cls);
        }
    }

    protected void e() {
        if (this.d != null) {
            try {
                this.d.c();
                this.d = null;
            } catch (IllegalStateException e) {
                e.d("AbsUpdateWizard", "In dismissDialog, Failed to dismiss the dialog.", true);
            }
        }
    }

    protected boolean a(boolean z) {
        Activity d = d();
        if (d == null) {
            return false;
        }
        ArrayList d2 = this.c.d();
        if (d2 != null && d2.size() > 0) {
            d2.remove(0);
        }
        if (this.b == null) {
            a(d2);
        }
        if (this.b == null) {
            return false;
        }
        this.e = true;
        this.c.a(d2);
        this.c.a(z);
        e.b("AbsUpdateWizard", "startNextWizard mBridgeActivityDelegate.onBridgeActivityCreate(activity)", true);
        this.b.a(d);
        return true;
    }

    private void a(ArrayList<Integer> arrayList) {
        String str = null;
        if (arrayList != null && arrayList.size() > 0) {
            str = a(((Integer) arrayList.get(0)).intValue());
        }
        if (str != null) {
            try {
                this.b = (com.huawei.hwid.activity.a) Class.forName(str).asSubclass(com.huawei.hwid.activity.a.class).newInstance();
            } catch (ClassCastException e) {
                e.d("AbsUpdateWizard", "getBridgeActivityDelegate ClassCastException error", true);
            } catch (InstantiationException e2) {
                e.d("AbsUpdateWizard", "getBridgeActivityDelegate InstantiationException error", true);
            } catch (IllegalAccessException e3) {
                e.d("AbsUpdateWizard", "getBridgeActivityDelegate IllegalAccessException error", true);
            } catch (ClassNotFoundException e4) {
                e.d("AbsUpdateWizard", "getBridgeActivityDelegate ClassNotFoundException error", true);
            }
        }
    }

    protected void a(int i, int i2) {
        Activity d = d();
        if (d != null && !d.isFinishing()) {
            Intent intent = new Intent();
            intent.putExtra(BridgeActivity.EXTRA_DELEGATE_CLASS_NAME, getClass().getName());
            intent.putExtra(BridgeActivity.EXTRA_RESULT, i);
            d.setResult(-1, intent);
            d.finish();
        }
    }

    public void a(int i, KeyEvent keyEvent) {
        if (this.e && this.b != null) {
            this.b.a(i, keyEvent);
        }
    }

    protected boolean a(String str, int i) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Context d = d();
        if (d == null || d.isFinishing() || new d(d).b(str) < i) {
            return false;
        }
        return true;
    }
}
